package com.example.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * 自定义适配器Adapter
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    //Item表
    private List<Imagetext> mImagetextList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        public ViewHolder(View view){
            //ViewHolder用作临时存储器。滚动时可快速设置值，不必重新创建对象，以提高性能。
            super(view);
            imageView=(ImageView)view.findViewById(R.id.image);
            textView=(TextView)view.findViewById(R.id.text);
        }
    }
    public RecyclerViewAdapter(List<Imagetext> imagetextList){mImagetextList=imagetextList;}
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        //临时保存holder布局
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }
    public void onBindViewHolder(ViewHolder holder,int position){
        //填充holder布局
        Imagetext imagetext=mImagetextList.get(position);
        holder.imageView.setImageResource(imagetext.getImageId());
        holder.textView.setText(imagetext.getText());
    }
    public int getItemCount(){return mImagetextList.size();}
}